﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WinNormal
{
    public partial class CombinFilesForm : Form
    {
        public CombinFilesForm()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 将多个小的文本文件,合并为若干个不超过指定大小的大文件.
        /// 记得是在看小说的时候有用到过.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRum_Click(object sender, EventArgs e)
        {
 
            DirectoryInfo DD = Directory.CreateDirectory(textBox1.Text);
            FileInfo[] FFColl=DD.GetFiles();
            int fileIndex = 1;
            string filetextcoll = "";
            int count = DD.GetFiles().Length;
            for (int i = 0; i < count; i++)
            {
                FileInfo ff = FFColl[i];

                FileStream fs1 = File.Open(ff.FullName, FileMode.Open);
                StreamReader sr = new StreamReader(fs1, System.Text.Encoding.GetEncoding("gb2312"));

                string filetext = sr.ReadToEnd();
                filetextcoll += filetext;
                if (filetextcoll.Length > 200000 || i == count - 1)
                {
                    //写文件
                    string newFileName = textBox2.Text + "\\File" + fileIndex.ToString() + ".txt";
                    FileStream fs = File.Create(newFileName);
                    StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"));
                    sw.Write(filetextcoll);
                    filetextcoll = "";
                    fileIndex++;
                }
            }
  
        }

 
    }
}
